
package cz.czu.pef.DataModelling.beans;

import java.util.*;
import java.util.logging.*;

import javax.annotation.*;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cz.czu.pef.DataModelling.beans.common.CalendarBean;
import cz.czu.pef.DataModelling.domainObjects.Person;
import cz.czu.pef.DataModelling.ejb.api.PersonEjbInterface;
import cz.czu.pef.common.CustomFilterField;
import cz.czu.pef.common.CustomSortField;

@Component
@ViewScoped
@ManagedBean
@Scope(value="view")
//@Scope(value="prototype")
public class PeopleBean extends AbstractExtendedTableBean<Person>
{
	private static final long serialVersionUID = 7241568721878550809L;
	public static final Logger logger = Logger.getLogger(PeopleBean.class.getName());

	private Person person;
	private CalendarBean newPersonCalendar = new CalendarBean();
	private PersonEjbInterface personEjb;

	@PostConstruct
	public void create()
	{
	  personEjb = this.applicationContext.getBean(PersonEjbInterface.class);
	  person = new Person();
	}

	public Person getPerson()
	{
		return person;
	}


	public void setPerson(Person person)
	{
		this.person = person;
	}

	public CalendarBean getNewPersonCalendar()
	{
		return newPersonCalendar;
	}


	public void setNewPersonCalendar(CalendarBean newPersonCalendar)
	{
		this.newPersonCalendar = newPersonCalendar;
	}

	public String createNew()
	{
		try
		{
			logger.finest("createNew():: Entry");
			/*
			FacesContext context = FacesContext.getCurrentInstance();
			//Person person = (Person)context.getApplication().evaluateExpressionGet(context, "#{person}", PersonImpl.class);
			Person newPerson = (PersonImpl) context.getApplication().getExpressionFactory().createValueExpression(context.getELContext(), "#{person}", PersonImpl.class).getValue(context.getELContext());
			*/

			logger.info("createNew():: person=" + person);
			if (person != null)
			{
				if (newPersonCalendar != null)
				{
					person.setBirthDate(newPersonCalendar.getSelectedDate());
				}
				if (person.getBirthDate() == null)
					person.setBirthDate(new Date());

				// persist new person
				personEjb.merge(person);
			}

			// reset detail form's data
			person = this.applicationContext.getBean(Person.class);
			newPersonCalendar = new CalendarBean(newPersonCalendar);

			return null;
		}
		finally
		{
			logger.finest("createNew():: Exit");

		}
	}

  @Override
  public boolean canBeRemoved(Person entity)
  {
    return personEjb.canBeRemoved(entity);
  }

  @Override
  public void removeById(long id)
  {
    personEjb.removeById(id);
  }

  @Override
  public Person getByStringId(String myKey)
  {
    return personEjb.getByStringId(myKey);
  }

  @Override
  public List<Person> findAllByRangeWithFilterAndSort(Integer firstRow, Integer endRow, List<CustomFilterField> filterFields, List<CustomSortField> sortFields)
  {
    return personEjb.findAllByRangeWithFilterAndSort(firstRow, endRow, filterFields, sortFields);
  }

  @Override
  public Long countWithFilter(List<CustomFilterField> filterFields)
  {
    return personEjb.countWithFilter(filterFields);
  }


}
